<template>
  <div class="login">
    <div class="login-weaper">
      <div class="login-left">
        <div class="login-time">{{ time.txt }}</div>
        <div class="login-left-box">
          <div>
            <div class="logo">{{ getThemeConfig.globalViceTitle }}</div>
            <h2 class="title">{{ getThemeConfig.globalViceDes }}</h2>
            <div class="msg">
              <div class="msg-author">
                <span>{{ quotations.name }}</span>
                <span>{{ quotations.comeFrom }}</span>
              </div>
              <div class="msg-txt">{{ quotations.content }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="login-main">
          <h4 class="login-title">{{ getThemeConfig.globalTitle }}</h4>
          <el-form ref="loginForm" :model="loginForm"  class="el-form login-form" :rules="loginRules">
            <el-form-item style="margin-left: 0" prop="userName">
              <el-input
                ref="username"
                v-model="loginForm.username"
                type="text"
                placeholder="gaocan"
                prefix-icon="el-icon-user"
                clearable
                autocomplete="off"
              />
            </el-form-item>
            <el-form-item style="margin-left: 0" prop="password">
              <el-input
                ref="password"
                v-model="loginForm.password"
                type="password"
                placeholder="123456"
                prefix-icon="el-icon-lock"
                autocomplete="off"
                :show-password="true"
              />
            </el-form-item>
            <el-form-item style="margin: 40px 0 0">
              <el-button type="primary" class="login-submit" :loading="loading" @click="handleLogin">
                <span>{{ $t('login.logIn') }}</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div class="vue-particles">
      <vue-particles color="#dedede" shapeType="star" linesColor="#dedede" hoverMode="grab" clickMode="push" style="height: 100%"></vue-particles>
    </div>
  </div>
</template>

<script>
import {validUsername} from '@/utils/validate'
import { formatDate, formatAxis } from '@/utils/formatTime'
// eslint-disable-next-line no-unused-vars
import { quotationsList } from './mock'


export default {
  name: 'Login',

  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    return {
      quotationsList,
      quotations: {},
      loginForm: {
        username: 'admin',
        password: '111111'
      },
      loginRules: {
        username: [{required: true, trigger: 'blur', validator: validateUsername}],
        password: [{required: true, trigger: 'blur', validator: validatePassword}]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      time: {
        txt: '',
        fun: null
      }
    }
  },
  computed: {
    // 获取当前时间
    currentTime() {
      return formatAxis(new Date())
    },
    // 获取布局配置信息
    getThemeConfig() {
      return this.$store.state.themeConfig.themeConfig
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    this.initTime()
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    this.initRandomQuotations()
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    initRandomQuotations() {
      this.quotations = this.quotationsList[Math.floor(Math.random() * this.quotationsList.length)];
    },
    initTime() {
      this.time.txt = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS WWW QQQQ');
      this.time.fun = setInterval(() => {
        this.time.txt = formatDate(new Date(), 'YYYY-mm-dd HH:MM:SS WWW QQQQ');
      }, 1000)
    },
    checkCapslock(e) {
      const {key} = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      console.log('点击登录1')
      // console.log(this.$refs.loginForm)
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = true
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style scoped lang="scss">
.login {
  height: 100%;
  width: 100%;
  overflow: hidden;
  display: flex;
  position: relative;
  .vue-particles {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(ellipse at top left, rgba(105, 155, 200, 1) 0%, rgba(181, 197, 216, 1) 57%);
  }
  .login-weaper {
    margin: auto;
    height: 500px;
    display: flex;
    box-sizing: border-box;
    position: relative;
    z-index: 1;
    border: none;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
    .login-left {
      width: 491px;
      padding: 20px;
      font-size: 16px;
      color: var(--prev-color-text-white);
      position: relative;
      background-color: var(--prev-color-primary);
      display: flex;
      flex-direction: column;
      border-top-left-radius: 4px;
      border-bottom-left-radius: 4px;
      .login-time {
        width: 100%;
        color: var(--prev-color-text-white);
        opacity: 0.9;
        font-size: 14px;
        overflow: hidden;
      }
      .login-left-box {
        flex: 1;
        overflow: hidden;
        position: relative;
        z-index: 1;
        display: flex;
        align-items: center;
        padding: 80px 45px;
        .logo {
          font-size: 22px;
          margin-bottom: 15px;
        }
        .title {
          color: var(--prev-color-text-white);
          font-weight: 300;
          letter-spacing: 2px;
          font-size: 16px;
        }
        .msg {
          color: var(--prev-color-text-white);
          font-size: 13px;
          margin-top: 35px;
          .msg-author {
            opacity: 0.6;
            span:last-of-type {
              margin-left: 15px;
            }
          }
          .msg-txt {
            margin-top: 15px;
            line-height: 22px;
          }
        }
      }
    }
    .login-right {
      width: 491px;
      padding: 20px;
      position: relative;
      align-items: center;
      display: flex;
      background-color: var(--prev-bg-white);
      border-top-right-radius: 4px;
      border-bottom-right-radius: 4px;
      .login-main {
        margin: 0 auto;
        width: 70%;
        .login-title {
          color: var(--prev-color-text-primary);
          margin-bottom: 40px;
          font-weight: 500;
          font-size: 22px;
          text-align: center;
          letter-spacing: 4px;
        }
        .login-form {
          margin: 10px 0;
          i {
            color: var(--prev-color-text-primary);
          }
          .el-form-item {
            margin-bottom: 20px !important;
            .login-code {
              display: flex;
              align-items: center;
              justify-content: space-around;
              margin: 0 0 0 10px;
              user-select: none;
              .login-code-img {
                margin-top: 2px;
                width: 100px;
                height: 38px;
                border: 1px solid var(--prev-border-color-base);
                color: var(--prev-color-text-primary);
                font-size: 14px;
                font-weight: 700;
                letter-spacing: 5px;
                line-height: 38px;
                text-indent: 5px;
                text-align: center;
                cursor: pointer;
                transition: all ease 0.2s;
                border-radius: 4px;
                &:hover {
                  border-color: var(--prev-border-color-hover);
                  transition: all ease 0.2s;
                }
              }
            }
            .login-submit {
              width: 100%;
              letter-spacing: 2px;
            }
          }
        }
        .login-menu {
          margin-top: 30px;
          width: 100%;
          text-align: left;
          a {
            color: var(--prev-color-text-secondary);
            font-size: 12px;
            margin: 0 8px;
            text-decoration: none;
            &:hover {
              color: var(--prev-color-primary);
              text-decoration: underline;
            }
          }
        }
      }
    }
  }
}
</style>



